<?php

/**
 * MLEPP - ManiaLive Extending Plugin Pack
 *
 * -- MLEPP Plugin --
 * @name Join/Leave Message
 * @date 04-01-2011
 * @version r1170
 * @website mlepp.trackmania.nl
 * @package MLEPP
 *
 * @author The MLEPP team
 * @copyright 2010 - 2011
 *
 * ---------------------------------------------------------------------
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------
 * You are allowed to change things or use this in other projects, as
 * long as you leave the information at the top (name, date, version,
 * website, package, author, copyright) and publish the code under
 * the GNU General Public License version 3.
 * ---------------------------------------------------------------------
 */

namespace ManiaLivePlugins\MLEPP\JoinLeaveMessage;

use ManiaLive\Utilities\Console;
use ManiaLive\DedicatedApi\Connection;
use ManiaLive\Data\Storage;
use ManiaLive\Features\Admin\AdminGroup;
use ManiaLive\Config\Loader;
use ManiaLivePlugins\MLEPP\Core\Core;
use ManiaLivePlugins\MLEPP\Core\Mlepp;

class JoinLeaveMessage extends \ManiaLive\PluginHandler\Plugin {

	public $rankingsEnabled = false;
	protected $rank;
	private $mlepp;
    /**
     * onInit()
     * Function called on initialisation of ManiaLive.
     *
     * @return void
     */

	function onInit() {
		$this->setVersion(1170);
        $this->setPublicMethod('getVersion');
      
	}

    /**
     * onLoad()
     * Function called on loading of ManiaLive.
     *
     * @return void
     */

	function onLoad() {
		$this->enableDedicatedEvents();
		$this->mlepp = Mlepp::getInstance();
		Console::println('['.date('H:i:s').'] [MLEPP] Plugin: JoinLeaveMessage r'.$this->getVersion() );
	}

	function onUnload() {
		parent::onUnload();
	}
	 /**
	 * onReady()
     * Function called when ManiaLive is ready loading.
	 *
	 * @return void
	 */

    function onReady() {
        if($this->isPluginLoaded('MLEPP\Rankings', 284)) {
            $this->rankingsEnabled = true;
        }
    }

	 /**
	 * onPlayerConnect()
     * Function called when a player connects.
	 *
	 * @param mixed $login
	 * @param mixed $isSpectator
	 * @return void
	 */

	function onPlayerConnect($login, $isSpectator) {
		$source_player = $this->storage->getPlayerObject($login);

		Console::println('['.date('H:i:s').'] [MLEPP] [JoinLeaveMessage] '.$login.' joins the server.');

		$message = $this->controlMsg(Config::getInstance()->standardJoinMsg, $source_player);
   		$admin_message = $this->controlMsg(Config::getInstance()->adminJoinMsg, $source_player);

		if($this->rankingsEnabled == true) {
			$rank = $this->callPublicMethod('MLEPP\Rankings', 'getRank', $login);
			if ($rank) {
				$message = $this->controlMsg(Config::getInstance()->rankedJoinMsg, $source_player);
				$admin_message = $this->controlMsg(Config::getInstance()->adminRankedJoinMsg, $source_player);
			}
		}


		if($this->mlepp->isPlayerOnline($login)) {
            // show join message to players
            foreach($this->storage->players as $login => $player) {
                if(in_array($login, $this->mlepp->AdminGroup->getAdmins() )){
                     $this->mlepp->sendChat($admin_message,$player,$this);
                } else {
                    $this->mlepp->sendChat($message,$player,$this);
                }
            }
        }

		// show join message also to spectators
		if($this->mlepp->isPlayerOnline($login)) {
            foreach($this->storage->spectators as $login => $player) {
                if(in_array($login, $this->mlepp->AdminGroup->getAdmins())){
                     $this->mlepp->sendChat($admin_message,$player,$this);
                } else {
                     $this->mlepp->sendChat($message,$player,$this);
                }
            }
        }
	}

	 /**
	 * onPlayerDisconnect()
     * Function called when a player disconnects.
	 *
	 * @param mixed $login
	 * @return void
	 */

	function onPlayerDisconnect($login) {
		Console::println('['.date('H:i:s').'] [MLEPP] [JoinLeaveMessage] '.$login.' left the server.');
        if($this->mlepp->isPlayerOnline($login)) {
            $source_player = $this->storage->getPlayerObject($login);
            $message = $this->controlMsg(Config::getInstance()->leaveMsg, $source_player);
		} else {
            $message = '%variable%»» %variable%'.$login.'$z$s%server% has left the server.';
		}
		$this->mlepp->sendChat($message,false,$this);
	}

	 /**
	 * controlMsg()
     * Helper function, used for parsing the join/leave messages.
	 *
	 * @param mixed $msg
	 * @param mixed $player
	 * @return
	 */

	function controlMsg($msg, $player) {
		if(isset($player->path) && is_string($player->path)) $path = str_replace('World|', '', $player->path);
		else $path = "unknown";

		if(isset($player->ladderStats['PlayerRankings'][0]['Ranking'])) {
			$ladderrank = $player->ladderStats['PlayerRankings'][0]['Ranking'];
			if(empty($ladderrank) || $ladderrank == -1 || $ladderrank == false ) $ladderrank = "n/a";
		} else {
			$ladderrank = "n/a";
		}

		if($player->isSpectator) {
			$spec = ' %server%(%variable%Spec)%server%';
		} else {
			$spec = '';
		}

        if(in_array($player->login,$this->mlepp->AdminGroup->getAdminsByGroup('root'))) {
            $title = $this->mlepp->AdminGroup->getTitle('root');
            //$title = "MasterAdmin";
        } elseif (in_array($player->login,$this->mlepp->AdminGroup->getAdminsByGroup('admin'))) {
            $title = $this->mlepp->AdminGroup->getTitle('admin');
			//$title = "Admin";
		} elseif (in_array($player->login,$this->mlepp->AdminGroup->getAdminsByGroup('operator'))) {
            $title = $this->mlepp->AdminGroup->getTitle('operator');
			//$title = "Operator";
		} else {
            $groups = $this->mlepp->AdminGroup->getAdminGroups($player->login);
            if(!empty($groups[0])) {
                $title = $this->mlepp->AdminGroup->getTitle($groups[0]);
            } else {
                $title = "Player";
            }
		}

		$message = $msg;
		$message = str_replace('%nickname%', $player->nickName, $message);
		$message = str_replace('%spec%', $spec, $message);
                $zone = explode("|",$path);
                if (isset($zone[0])) {
                    $country = $zone[0];
                }
                else {
                    $country = $path;
                }
                $message = str_replace('%country%', $country, $message);
    $message = str_replace('%country_short%', $this->mapCountry($country), $message);
    $message = str_replace('%zone%', $path, $message);
		$message = str_replace('%ladderrank%', number_format((int)$ladderrank, 0, '', ' '), $message);
		$message = str_replace('%login%', $player->login, $message);
		$ip = explode(":",$player->iPAddress);
		$message = str_replace('%ip%', $ip[0], $message);
		$message = str_replace('%version%', $player->clientVersion, $message);
		$message = str_replace('%title%', $title, $message);

        if($this->rankingsEnabled == true) {
            $rank = $this->callPublicMethod('MLEPP\Rankings', 'getRank', $player->login);
            if($rank) {
                $serverrank = $rank['rank'];
				$average = $rank['avg'];
				$totalranks = $rank['players'];
            } else {
                $serverrank = '--';
				$average = '--';
				$totalranks = '--';
            }
			$message = str_replace('%serverrank%', $serverrank, $message);
			$message = str_replace('%totalranks%', $totalranks, $message);
			$message = str_replace('%average%', $average, $message);
        }

		return $message;
	}

/**
* Map country names to 3-letter Nation abbreviations
* Created by Xymph
* Based on http://en.wikipedia.org/wiki/List_of_IOC_country_codes
*/
function mapCountry($country)
	{
	$nations = array(
		'Afghanistan' => 'AFG',
		'Albania' => 'ALB',
		'Algeria' => 'ALG',
		'Andorra' => 'AND',
		'Angola' => 'ANG',
		'Argentina' => 'ARG',
		'Armenia' => 'ARM',
		'Aruba' => 'ARU',
		'Australia' => 'AUS',
		'Austria' => 'AUT',
		'Azerbaijan' => 'AZE',
		'Bahamas' => 'BAH',
		'Bahrain' => 'BRN',
		'Bangladesh' => 'BAN',
		'Barbados' => 'BAR',
		'Belarus' => 'BLR',
		'Belgium' => 'BEL',
		'Belize' => 'BIZ',
		'Benin' => 'BEN',
		'Bermuda' => 'BER',
		'Bhutan' => 'BHU',
		'Bolivia' => 'BOL',
		'Bosnia&Herzegovina' => 'BIH',
		'Botswana' => 'BOT',
		'Brazil' => 'BRA',
		'Brunei' => 'BRU',
		'Bulgaria' => 'BUL',
		'Burkina Faso' => 'BUR',
		'Burundi' => 'BDI',
		'Cambodia' => 'CAM',
		'Cameroon' => 'CAR',  // actually CMR
		'Canada' => 'CAN',
		'Cape Verde' => 'CPV',
		'Central African Republic' => 'CAF',
		'Chad' => 'CHA',
		'Chile' => 'CHI',
		'China' => 'CHN',
		'Chinese Taipei' => 'TPE',
		'Colombia' => 'COL',
		'Congo' => 'CGO',
		'Costa Rica' => 'CRC',
		'Croatia' => 'CRO',
		'Cuba' => 'CUB',
		'Cyprus' => 'CYP',
		'Czech Republic' => 'CZE',
		'Czech republic' => 'CZE',
		'DR Congo' => 'COD',
		'Denmark' => 'DEN',
		'Djibouti' => 'DJI',
		'Dominica' => 'DMA',
		'Dominican Republic' => 'DOM',
		'Ecuador' => 'ECU',
		'Egypt' => 'EGY',
		'El Salvador' => 'ESA',
		'Eritrea' => 'ERI',
		'Estonia' => 'EST',
		'Ethiopia' => 'ETH',
		'Fiji' => 'FIJ',
		'Finland' => 'FIN',
		'France' => 'FRA',
		'Gabon' => 'GAB',
		'Gambia' => 'GAM',
		'Georgia' => 'GEO',
		'Germany' => 'GER',
		'Ghana' => 'GHA',
		'Greece' => 'GRE',
		'Grenada' => 'GRN',
		'Guam' => 'GUM',
		'Guatemala' => 'GUA',
		'Guinea' => 'GUI',
		'Guinea-Bissau' => 'GBS',
		'Guyana' => 'GUY',
		'Haiti' => 'HAI',
		'Honduras' => 'HON',
		'Hong Kong' => 'HKG',
		'Hungary' => 'HUN',
		'Iceland' => 'ISL',
		'India' => 'IND',
		'Indonesia' => 'INA',
		'Iran' => 'IRI',
		'Iraq' => 'IRQ',
		'Ireland' => 'IRL',
		'Israel' => 'ISR',
		'Italy' => 'ITA',
		'Ivory Coast' => 'CIV',
		'Jamaica' => 'JAM',
		'Japan' => 'JPN',
		'Jordan' => 'JOR',
		'Kazakhstan' => 'KAZ',
		'Kenya' => 'KEN',
		'Kiribati' => 'KIR',
		'Korea' => 'KOR',
		'Kuwait' => 'KUW',
		'Kyrgyzstan' => 'KGZ',
		'Laos' => 'LAO',
		'Latvia' => 'LAT',
		'Lebanon' => 'LIB',
		'Lesotho' => 'LES',
		'Liberia' => 'LBR',
		'Libya' => 'LBA',
		'Liechtenstein' => 'LIE',
		'Lithuania' => 'LTU',
		'Luxembourg' => 'LUX',
		'Macedonia' => 'MKD',
		'Malawi' => 'MAW',
		'Malaysia' => 'MAS',
		'Mali' => 'MLI',
		'Malta' => 'MLT',
		'Mauritania' => 'MTN',
		'Mauritius' => 'MRI',
		'Mexico' => 'MEX',
		'Moldova' => 'MDA',
		'Monaco' => 'MON',
		'Mongolia' => 'MGL',
		'Montenegro' => 'MNE',
		'Morocco' => 'MAR',
		'Mozambique' => 'MOZ',
		'Myanmar' => 'MYA',
		'Namibia' => 'NAM',
		'Nauru' => 'NRU',
		'Nepal' => 'NEP',
		'Netherlands' => 'NED',
		'New Zealand' => 'NZL',
		'Nicaragua' => 'NCA',
		'Niger' => 'NIG',
		'Nigeria' => 'NGR',
		'Norway' => 'NOR',
		'Oman' => 'OMA',
		'Other Countries' => 'OTH',
		'Pakistan' => 'PAK',
		'Palau' => 'PLW',
		'Palestine' => 'PLE',
		'Panama' => 'PAN',
		'Paraguay' => 'PAR',
		'Peru' => 'PER',
		'Philippines' => 'PHI',
		'Poland' => 'POL',
		'Portugal' => 'POR',
		'Puerto Rico' => 'PUR',
		'Qatar' => 'QAT',
		'Romania' => 'ROM',  // actually ROU
		'Russia' => 'RUS',
		'Rwanda' => 'RWA',
		'Samoa' => 'SAM',
		'San Marino' => 'SMR',
		'Saudi Arabia' => 'KSA',
		'Senegal' => 'SEN',
		'Serbia' => 'SCG',  // actually SRB
		'Sierra Leone' => 'SLE',
		'Singapore' => 'SIN',
		'Slovakia' => 'SVK',
		'Slovenia' => 'SLO',
		'Somalia' => 'SOM',
		'South Africa' => 'RSA',
		'Spain' => 'ESP',
		'Sri Lanka' => 'SRI',
		'Sudan' => 'SUD',
		'Suriname' => 'SUR',
		'Swaziland' => 'SWZ',
		'Sweden' => 'SWE',
		'Switzerland' => 'SUI',
		'Syria' => 'SYR',
		'Taiwan' => 'TWN',
		'Tajikistan' => 'TJK',
		'Tanzania' => 'TAN',
		'Thailand' => 'THA',
		'Togo' => 'TOG',
		'Tonga' => 'TGA',
		'Trinidad and Tobago' => 'TRI',
		'Tunisia' => 'TUN',
		'Turkey' => 'TUR',
		'Turkmenistan' => 'TKM',
		'Tuvalu' => 'TUV',
		'Uganda' => 'UGA',
		'Ukraine' => 'UKR',
		'United Arab Emirates' => 'UAE',
		'United Kingdom' => 'GBR',
		'United States of America' => 'USA',
		'Uruguay' => 'URU',
		'Uzbekistan' => 'UZB',
		'Vanuatu' => 'VAN',
		'Venezuela' => 'VEN',
		'Vietnam' => 'VIE',
		'Yemen' => 'YEM',
		'Zambia' => 'ZAM',
		'Zimbabwe' => 'ZIM',
		);

	if (array_key_exists($country, $nations))
		{
		$nation = $nations[$country];
		}
	else
		{
		$nation = $country;

		if ($country != '')
			trigger_error('Could not map country: ' . $country, E_USER_WARNING);
		}

	return $nation;
	} // mapCountry

}
?>